<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Message extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->check_authentication(null, true);
		$this->load->model('Message_model', 'message');
		$this->lang->load('message', $this->session->userdata['user_lang']);
	}
	
	public function index()
	{
		$phone_list = $this->phone->get(array('idcandidate' => $this->session->userdata['idprofile']));
		$grid_phone = array();
        
        foreach ($phone_list->result() as $row)
		{
			$grid_phone[] = '<div class="row"><span>(' . $row->region . ') ' . $row->number .
			'</span><div class="gtool"><a onclick="CandidatePhone.editPhone('.$row->id.');">'.$this->lang->line('global_update').'</a>
			 &middot; <a onclick="CandidatePhone.deletePhone('.$row->id.');">'.$this->lang->line('global_delete').'</a></div></div>';
		}
		
		if (!$grid_phone) {
			 $data['grid_phone'] = '<center>'.$this->lang->line('phone_noresults').'</center>';
		} else {
			 $data['grid_phone'] = implode('<hr/>', $grid_phone);
		}
		
		exitHTML($data['grid_phone']);
	}
	
	public function insert()
	{
		$data = $_POST;
		
		if (missing_field($data, array('member_to', 'message')))
		{
			formError($this->lang->line('message_missingfield'), true);
		}
		
		$data['idmemberto'] = $data['member_to'];
		$data['idmembertotype'] = $data['member_type'];
		$data['idmemberfrom'] = $this->session->userdata['idmember'];
		$data['idmemberfromtype'] = $this->session->userdata['idtype'];
		$data['idtype'] = is_numeric($data['message_type']) ? $data['message_type'] : 3;
		switch ($data['idtype'])
		{
			case 1:
			case 4:
				$data['idsubscription'] = $data['item_id'];
				break;
				
			case 2:
				$data['idjob'] = $data['item_id'];
				break;
		}
		$data['idmessageabove'] = $data['message_above'];
		
		if ($this->message->insert($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true);
		}
		else
		{
			exitJs('closeBox();');
		}
	}
	
	public function delete()
	{
		$data = $_POST;
		
		if (missing_field($data, array('id')))
		{
			formError($this->lang->line('message_missingfield'));
		}
		
		if ($this->phone->delete(array('id' => $data['id'])) === false)
		{
		   formError($this->lang->line('global_systemfail'));
		}
		else
		{
			exitJs('CandidatePhone.getPhoneGrid();');
		}
	}
	
	public function edit($pars = '')
	{
		header("Content-type: text/html; charset=utf-8");
		
		$array_pars = explode('-', $pars);
		
		$data = $_POST;
		$data['message_above'] = $array_pars[0];
		$data['member_to'] = $array_pars[1];
		$data['member_type'] = $array_pars[2];
		$data['message_type'] = $array_pars[3];
		$data['item_id'] = $array_pars[4];
				
		$data['userdata'] = $this->session->userdata;
        $this->load->view('message/form', $data);
	}
	
	public function add($pars = '')
	{
		$this->edit($pars);
	}
	
	public function reply($pars = '')
	{
		$this->edit($pars);
	}
}

?>